package com.ruoyi.iot.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.iot.service.impl.IotEKuiperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * eKuiper规则管理Controller
 *
 * @author Howe
 * @since 2025/7/17
 */
@RestController
@RequestMapping("/iot/ekuiper")
public class IotEKuiperController {

  @Autowired
  private IotEKuiperService iotEKuiperService;

  /**
   * 创建 eKuiper 规则
   *
   * @param ruleName 规则名称
   * @param sql      规则SQL
   * @param actions  动作(JSON字符串)
   * @return AjaxResult
   */
  @PostMapping("/createRule")
  public AjaxResult createRule(@RequestParam String ruleName,
      @RequestParam String sql,
      @RequestParam String actions) {
    String result = iotEKuiperService.createRule(ruleName, sql, actions);
    return AjaxResult.success(result);
  }

  /**
   * 删除 eKuiper 规则
   *
   * @param ruleName 规则名称
   * @return AjaxResult
   */
  @DeleteMapping("/deleteRule/{ruleName}")
  public AjaxResult deleteRule(@PathVariable String ruleName) {
    iotEKuiperService.deleteRule(ruleName);
    return AjaxResult.success();
  }

  /**
   * 查询 eKuiper 规则
   *
   * @param ruleName 规则名称
   * @return AjaxResult
   */
  @GetMapping("/getRule/{ruleName}")
  public AjaxResult getRule(@PathVariable String ruleName) {
    String result = iotEKuiperService.getRule(ruleName);
    return AjaxResult.success(result);
  }

  /**
   * 查询 eKuiper 中所有规则
   *
   * @return AjaxResult
   * @author Howe
   * @since 2025/7/17
   */
  @GetMapping("/listRules")
  public AjaxResult listRules() {
    String result = iotEKuiperService.getAllRules();
    return AjaxResult.success(result);
  }
}